package io.renren.modules.jsf.service;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import io.renren.modules.jsf.entity.vo.PayVO;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

import static io.renren.common.utils.ShiroUtils.getUserId;

@Service
public class PayService {

    @Value("${alipay.returnUrl}")
    private String returnUrl;

    @Autowired
    SysUserService userService;

    /**
     * 下单支付
     *
     * @return 结果
     * @throws Exception 异常
     */
    public Object pay(PayVO payVO) throws Exception {
        System.out.println(returnUrl);
        AlipayTradePagePayResponse response = Factory.Payment.Page().pay(
                payVO.getUsername() + "余额充值",
                UUID.randomUUID().toString().substring(0, 10),
                payVO.getMoney().toString(),
                returnUrl);

        SysUserEntity userEntity = userService.selectOneByName(payVO.getUsername());
        userEntity.setMoney(userEntity.getMoney() + payVO.getMoney());
        userEntity.setPassword("123456");
        userService.update1(userEntity);
        return response.body;
    }
}
